import { getMine } from '../../api/user'
const app = getApp()
Page({
  data: {
    scrollViewHeight: 0,
    userAvatar: wx.getStorageSync('userAvatar') || '',
    userNickName: wx.getStorageSync('userNickName') || '',
    token: '',
    mainEntrance: [
      { name: '我的品赏', mark: 'pin', imgUrl: '/static/imgs/pin.png', url: '' },
      { name: '修书立魂', mark: 'xiu', imgUrl: '/static/imgs/xiu.png', url: '/packageA/pages/motto/motto' },
      { name: '参与的活动', mark: 'act', imgUrl: '/static/imgs/act.png', url: '/packageA/pages/myActivity/myActivity' }
    ],
    entrance: [
      { name: '个人空间', url: '/packageA/pages/personalSpace/personalSpace' },
      // { name: '圈子', url: '/packageA/pages/contacts/contacts' },
      // { name: '消费记录', url: '/packageA/pages/consumptionRecords/index' },
      { name: '我的收藏', url: '/packageA/pages/collection/collection' },
      { name: '考证报名', url: '/packageA/pages/textualResearchList/textualResearchList' },
      { name: '设置', url: '/packageA/pages/setting/setting' }
    ],
    counts: {
      teachingDesignerCount: 0,
      teachingResearchCount: 0,
      webCastCount: 0
    }
  },

  onLoad: function (options) {
    this.setData({
      token: wx.getStorageSync('token')
    })
    const _this = this
    wx.getSystemInfo({
      success: function(res) {
        _this.setData({
          scrollViewHeight: res.windowHeight
        })
      }
    });

    this.getAuth()
    if (this.data.token !== '' && this.data.token !== null) {
      this.getCount()
    }
  },
  onShow() {
    this.setData({
      token: wx.getStorageSync('token'),
      userAvatar: wx.getStorageSync('userAvatar') || '',
      userNickName: wx.getStorageSync('userNickName') || '',
    })
  },
  getAuth() {
    const _this = this
    return new Promise(resolve => {
      // 查看是否授权
      wx.getSetting({
        success (res){
          if (res.authSetting['scope.userInfo']) {
            if (!wx.getStorageSync('userAvatar')) {
              _this.getUserInfo()
            }
          }
        }
      })
      resolve()
    })
  },
  getCount() {
    getMine().then(res => {
      this.setData({
        counts: res
      })
    })
  },
  getUserInfo() {
    const _this = this
    wx.getUserInfo({
      success: function(res) {
        _this.setData({
          userNickName: res.userInfo.nickName,
          userAvatar: res.userInfo.avatarUrl
        })
        wx.setStorageSync('userAvatar', res.userInfo.avatarUrl)
        wx.setStorageSync('userNickName', res.userInfo.nickName)
      }
    })
  },
  toPageMessage() {
    wx.navigateTo({ url: '/packageA/pages/message/message' })
  },
  navigateTo({ currentTarget }) {
    if (!currentTarget.dataset.url) {
      wx.showToast({
        title: '此功能正在开发中~~~',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.navigateTo({ url: currentTarget.dataset.url })
    }
  },
  bindLogin() {
    this.getAuth().then(res => {
      app.bindLogin().then(loginRes => {
        getApp().globalData.token = loginRes;
        this.setData({
          token: loginRes
        })
        console.log(loginRes, 'res')
        console.log(wx.getStorageSync('token'),'token')
        // 获取统计数据
        this.getCount()
        // 获取注册进度
        app.getSignUpProgress()
      })
    })
  }
})
